package com.unnamed.b.atv.sample.ability;

import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.sample.ResourceTable;
import com.unnamed.b.atv.sample.holder.IconTreeItemHolder;
import com.unnamed.b.atv.sample.holder.ProfileHolder;
import com.unnamed.b.atv.sample.holder.SelectableHeaderHolder;
import com.unnamed.b.atv.sample.holder.SelectableItemHolder;
import com.unnamed.b.atv.view.OhosTreeView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.dialog.ToastDialog;

/**
 * SelectableTreeAbility
 *
 * @author Administrator
 * @Date 2021/5/26
 */
public class SelectableTreeAbility extends Ability {
    /**
     * 是否选择
     */
    private boolean selectionModeEnabled = false;
    private OhosTreeView treeView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_selectable_nodes);
        initView();
    }

    private void initView() {
        ComponentContainer dependentLayout = (ComponentContainer) findComponentById(ResourceTable.Id_container_select);
        Button selectionModeButton = (Button) findComponentById(ResourceTable.Id_btn_toggleSelection);
        selectionModeButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                selectionModeEnabled = !selectionModeEnabled;
                treeView.setSelectionModeEnabled(selectionModeEnabled);
            }
        });
        Button selectAllBtn = (Button) findComponentById(ResourceTable.Id_btn_selectAll);
        selectAllBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!selectionModeEnabled) {
                    new ToastDialog(getContext()).setText("Enable selection mode first").show();
                }
                treeView.selectAll(true);
            }
        });
        Button deselectAll = (Button) findComponentById(ResourceTable.Id_btn_deselectAll);
        deselectAll.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!selectionModeEnabled) {
                    new ToastDialog(getContext()).setText("Enable selection mode first").show();
                }
                treeView.deselectAll();
            }
        });
        Button check = (Button) findComponentById(ResourceTable.Id_btn_checkSelection);
        check.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!selectionModeEnabled) {
                    new ToastDialog(getContext()).setText("Enable selection mode first").show();
                } else {
                    new ToastDialog(getContext()).setText(treeView.getSelected().size() + " selected").show();
                }
            }
        });
        TreeNode root = TreeNode.root();
        TreeNode s1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_SDCard, "Storage1"))
            .setViewHolder(new ProfileHolder(getContext()));
        TreeNode s2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_SDCard, "Storage2"))
            .setViewHolder(new ProfileHolder(getContext()));
        TreeNode s3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_SDCard, "Storage3"))
            .setViewHolder(new ProfileHolder(getContext()));
        s1.setSelectable(false);
        s2.setSelectable(false);
        TreeNode folder1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_Files, "Folder 100"))
            .setViewHolder(new SelectableHeaderHolder(getContext()));
        TreeNode folder2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_Files, "Folder 200"))
            .setViewHolder(new SelectableHeaderHolder(getContext()));
        TreeNode folder3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_Files, "Folder 300"))
            .setViewHolder(new SelectableHeaderHolder(getContext()));
        TreeNode folder4 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_Files, "Folder 400"))
            .setViewHolder(new SelectableHeaderHolder(getContext()));
        fillFolder(folder1);
        fillFolder(folder2);
        fillFolder(folder3);
        fillFolder(folder4);
        s1.addChildren(folder1, folder2);
        s2.addChildren(folder3);
        s3.addChildren(folder4);
        root.addChildren(s1, s2, s3);
        treeView = new OhosTreeView(getContext(), root);
        dependentLayout.addComponent(treeView.getView());
    }

    /**
     * 设置数据
     *
     * @param folder 添加视图
     */
    private void fillFolder(TreeNode folder) {
        TreeNode file1 = new TreeNode("File1").setViewHolder(new SelectableItemHolder(getContext()));
        TreeNode file2 = new TreeNode("File2").setViewHolder(new SelectableItemHolder(getContext()));
        TreeNode file3 = new TreeNode("File3").setViewHolder(new SelectableItemHolder(getContext()));
        folder.addChildren(file1, file2, file3);
    }

}
